// 本地存储
function setStorage(key, val) {
	try {
		return uni.setStorageSync(key, val)
	} catch {
		console.log("setStorageSync --- ERROR")
	}
}

function getStorage(key) {
	try {
		return uni.getStorageSync(key)
	} catch (e) {
		console.log("getStorageSync --- ERROR")
	}
}
// 跳转
function navTo(url, status) {
	if (status == 1) {
		uni.showToast({
			title: '开发中...',
			icon: 'none'
		})
	} else if (status == 2) {
		uni.navigateBack({
			delta: 1
		})
	} else if (status == 3) {
		uni.switchTab({
			url: url
		})
	} else if (status == 4) {
		uni.redirectTo({
			url: url
		})
	} else {
		uni.navigateTo({
			url: url
		})
	}
}

// 消息通知
function showToast(msg, duration = 2100, icon = "none") {
	uni.showToast({
		title: msg,
		duration: duration,
		icon: icon,
	})
}

function showModel(title, content) {
	uni.showModal({
		title,
		content,
		success(res) {
			if (res.confirm) {
				console.log('用户点击确定');
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}

// 上传图片
function chooseImage(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count,
			sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ["album", "camera"], //从相册选择
			success: (res) => {
				resolve(res.tempFilePaths)
			},
		});
	})
}
// 返回上一页并且刷新
function goback(){
	let pages = getCurrentPages(); // 当前页面
	let beforePage = pages[pages.length - 2]; // 上一页
	uni.navigateBack({
	    success: function() {
	        beforePage.onLoad(); // 执行上一页的onLoad方法
	    }
	});
}
// 上传附件
function chooseMessageFile(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseFile({
			count: count,
			type: "all",
			extension: [
				"xlsx",
				"xls",
				"doc",
				"docx",
				"zip",
				"rar",
				"war",
				"png",
				"jpg",
				"pdf",
			],
			success: (res) => {
				resolve(res.tempFiles)
			},
		});
	})
}

// 复制剪贴板
function setClipboardData(data) {
	uni.setClipboardData({
		data,
		success: () => {
			console.log('success');
		}
	});
}

// 获取 null 内容区域高度
function contentHight(height = 0) {
	return `calc(100vh - ${getStorage('navbar') + height}px)`
}
// 验证手机号
function phoneumber(e) {
	let box = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
	return box.test(e)
}

function IDcard(e) {
	let IDcard = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	return IDcard.test(e)
}

// 秒转换成时分秒
function formatSeconds(value) {

	var theTime = parseInt(value); // 秒
	var middle = 0; // 分
	var hour = 0; // 小时

	if (theTime > 60) {
		middle = parseInt(theTime / 60);
		theTime = parseInt(theTime % 60);
		if (middle > 60) {
			hour = parseInt(middle / 60);
			middle = parseInt(middle % 60);
		}
	}
	var result = "" + parseInt(theTime) + "秒";
	if (middle > 0) {
		result = "" + parseInt(middle) + "分" + result;
	}
	if (hour > 0) {
		result = "" + parseInt(hour) + "小时" + result;
	}
	return result;
}
// 支付
function requestPayment(obj) {
    var obj = {
        // 服务提供商
        provider: "wxpay",
        // 订单数据
        orderInfo: obj.out_trade_no,
        // 统一下单接口返回的 prepay_id 参数值
        package: obj.package,
        // 小程序的appId
        appId: obj.appId,
        // 随机字符
        nonceStr: obj.nonceStr,
        // 时间戳
        timeStamp: obj.timeStamp,
        // 签名
        paySign: obj.paySign,
        // 算法签名
        signType: obj.signType,
    };
    return new Promise((resolve, reject) => {
        uni.requestPayment({
            ...obj,
            success: (res) => {
                resolve(JSON.stringify(res))
            },
            fail: (err) => {
                reject(JSON.stringify(err))
            },
        });
    })

}




module.exports = {
	setStorage: setStorage,
	getStorage: getStorage,
	navTo: navTo,
	showToast: showToast,
	showModel: showModel,
	chooseImage: chooseImage,
	chooseMessageFile: chooseMessageFile,
	contentHight: contentHight,
	setClipboardData: setClipboardData,
	phoneumber: phoneumber,
	IDcard: IDcard,
	formatSeconds:formatSeconds,
	requestPayment:requestPayment,
	goback:goback
}
